/**
 * @Title: UnitsModal.js
 * @Description: UnitsModal
 * @author: Fanchao
 * @date: 2020/8/15 5:45 下午
 * @version V1.0
 */
import React from 'react';
import { connect } from 'umi';
import { Form, Input, Modal, Select } from 'antd';

const { Option } = Select;

const layout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 20 },
};
const UnitsModal = ({ units: { modalVisible, superiorDepartmentId }, dispatch }) => {
    const [form] = Form.useForm();
    return (
        <Modal
            title='新增单位'
            visible={modalVisible}
            onOk={() => {
                form.validateFields().then(values => {
                    values.superiorDepartmentId = superiorDepartmentId;
                    if (superiorDepartmentId === 0) {
                        dispatch({
                            type: 'units/addFirstLevelDepartment',
                            payload: values,
                            callback: (result) => {
                                if (result) {
                                    dispatch({
                                        type: 'units/getUnitsTreeData',
                                        payload: {},
                                    });
                                    dispatch({
                                        type: 'units/setModalVisible',
                                        payload: false,
                                    });
                                    form.resetFields();
                                }
                            },
                        });
                    } else {
                        dispatch({
                            type: 'units/addLowerDepartment',
                            payload: values,
                            callback: (result) => {
                                if (result) {
                                    dispatch({
                                        type: 'units/getUnitsTreeData',
                                        payload: {},
                                    });
                                    dispatch({
                                        type: 'units/setModalVisible',
                                        payload: false,
                                    });
                                    form.resetFields();
                                }
                            },
                        });
                    }
                }).catch(info => {
                });
            }}
            onCancel={() => {
                form.resetFields();
                dispatch({
                    type: 'units/setModalVisible',
                    payload: false,
                });
            }}
        >
            <Form
                {...layout}
                layout="horizontal"
                form={form}
                name="basic"
                // 表单默认值，只有初始化以及重置时生效 莫用
                initialValues={{
                    // name: itemData.name
                }}
            >
                <Form.Item
                    label="单位名称"
                    name="departmentName"
                    rules={[{ required: true, message: '请输入单位名称' }]}
                >
                    <Input/>
                </Form.Item>
                <Form.Item
                    label="单位类型"
                    name="departmentType"
                    rules={[{ required: true, message: '请选择单位类型' }]}
                >
                    <Select
                        placeholder="请选择单位类型"
                    >
                        <Option value="MANAGE">管理单位</Option>
                        <Option value="TEACH">教学单位</Option>
                        <Option value="OFFICE">机关部门</Option>
                    </Select>
                </Form.Item>
                <Form.Item
                    label="备注"
                    name="comment"
                    rules={[{ required: false, message: '请添加描述' }]}
                >
                    <Input.TextArea/>
                </Form.Item>
            </Form>
        </Modal>
    );
};

export default connect(({ units, dispatch }) => ({
    units,
    dispatch,
}))(UnitsModal);
